//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CPICashFlow.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Index.h>
#include <gen/QL/Cashflows/IndexedCashFlow.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (Double notional, Cephei::QL::Indexes::IZeroInflationIndex^ index, DateTime baseDate, Double baseFixing, DateTime fixingDate, DateTime paymentDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ growthOnly, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>^ interpolation, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency) : CIndexedCashFlow(CCPICashFlow::typeid)
{
    CZeroInflationIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phCPICashFlow = NULL;
#endif
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //d
        _Cindex = safe_cast<CZeroInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::ZeroInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Date _baseDate = (QuantLib::Date)ValueHelper::Convert (baseDate); //d
        QuantLib::Real _baseFixing = (QuantLib::Real)ValueHelper::Convert (baseFixing); //d
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate); //d
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate); //d
        bool _growthOnly = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (growthOnly) ? (bool)ValueHelper::Convert (growthOnly->Value) : false); //4
        QuantLib::CPI::InterpolationType _interpolation = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>::IsSome::get (interpolation) ? (QuantLib::CPI::InterpolationType)interpolation->Value : QuantLib::CPI::InterpolationType::AsIndex); //10
        QuantLib::Frequency _frequency = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (frequency) ? (QuantLib::Frequency)frequency->Value : QuantLib::Frequency::NoFrequency); //10
        _ppCPICashFlow = new boost::shared_ptr<QuantLib::CPICashFlow> (new QuantLib::CPICashFlow ( _notional,  _index,  _baseDate,  _baseFixing,  _fixingDate,  _paymentDate,  _growthOnly,  _interpolation,  _frequency ));
        SetIndexedCashFlow (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (boost::shared_ptr<QuantLib::CPICashFlow>& childNative, Object^ owner) : CIndexedCashFlow(CCPICashFlow::typeid)
{
#ifdef HANDLE
	_phCPICashFlow = NULL;
#endif
	_ppCPICashFlow = &childNative;
    _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
}
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (QuantLib::CPICashFlow& childNative, Object^ owner) : CIndexedCashFlow(CCPICashFlow::typeid)
{
#ifdef HANDLE
	_phCPICashFlow = NULL;
#endif
	_ppCPICashFlow = new boost::shared_ptr<QuantLib::CPICashFlow> (&childNative);
    _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
    _CPICashFlowOwner = owner;
    _IndexedCashFlowOwner = owner;
}

Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (CCPICashFlow^ copy) : CIndexedCashFlow(CCPICashFlow::typeid)
{
#ifdef HANDLE
	_phCPICashFlow = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCPICashFlow = new boost::shared_ptr<QuantLib::CPICashFlow> (copy->GetShared());
        _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
    }
}
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (PLATFORM::Type^ t) : CIndexedCashFlow(CCPICashFlow::typeid)
{
#ifdef HANDLE
	_phCPICashFlow = NULL;
#endif
	if (!t->IsSubclassOf(CCPICashFlow::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (QuantLib::Handle<QuantLib::CPICashFlow>& childNative, Object^ owner)  : CIndexedCashFlow(CCPICashFlow::typeid)
{
	_phCPICashFlow = &childNative;
	_ppCPICashFlow = &static_cast<boost::shared_ptr<QuantLib::CPICashFlow>>(childNative.currentLink());
    _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
    _CPICashFlowOwner = owner;
}
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (QuantLib::Handle<QuantLib::CPICashFlow> childNative)  : CIndexedCashFlow(CCPICashFlow::typeid)
{
	_phCPICashFlow = &childNative;
	_ppCPICashFlow = &static_cast<boost::shared_ptr<QuantLib::CPICashFlow>>(childNative.currentLink());
    _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCPICashFlow::CCPICashFlow (QuantLib::CPICashFlow childNative)  : CIndexedCashFlow(CCPICashFlow::typeid)
{
#ifdef HANDLE
	_phCPICashFlow = NULL;
#endif
	_ppCPICashFlow = new boost::shared_ptr<QuantLib::CPICashFlow> (new QuantLib::CPICashFlow (childNative));
    _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (boost::dynamic_pointer_cast<QuantLib::IndexedCashFlow> (*_ppCPICashFlow));
}
#endif

Cephei::QL::Cashflows::CCPICashFlow::~CCPICashFlow ()
{
    if (_ppCPICashFlow != NULL)
    {
	    delete _ppCPICashFlow;
        _ppCPICashFlow = NULL;
    }
}
Cephei::QL::Cashflows::CCPICashFlow::!CCPICashFlow ()
{
    if (_ppCPICashFlow != NULL)
    {
	    delete _ppCPICashFlow;
    }
}
QuantLib::CPICashFlow& Cephei::QL::Cashflows::CCPICashFlow::GetReference ()
{
    if (_ppCPICashFlow == NULL) throw REFNEW NativeNullException ();
	return **_ppCPICashFlow;
}
boost::shared_ptr<QuantLib::CPICashFlow>& Cephei::QL::Cashflows::CCPICashFlow::GetShared ()
{
    if (_ppCPICashFlow == NULL) throw REFNEW NativeNullException ();
	return *_ppCPICashFlow;
}
QuantLib::CPICashFlow* Cephei::QL::Cashflows::CCPICashFlow::GetPointer ()
{
    if (_ppCPICashFlow == NULL) throw REFNEW NativeNullException ();
	return &**_ppCPICashFlow;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CPICashFlow>& Cephei::QL::Cashflows::CCPICashFlow::GetHandle ()
{
	if (_phCPICashFlow == NULL)
	{
		_phCPICashFlow = new Handle<QuantLib::CPICashFlow> (*_ppCPICashFlow);
	}
	return *_phCPICashFlow;
}
#endif
bool Cephei::QL::Cashflows::CCPICashFlow::HasNative () 
{
	return (_ppCPICashFlow != NULL);
}

Double Cephei::QL::Cashflows::CCPICashFlow::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPICashFlow)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CCPICashFlow::BaseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCPICashFlow)->baseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICashFlow::BaseFixing::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPICashFlow)->baseFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::FrequencyEnum Cephei::QL::Cashflows::CCPICashFlow::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppCPICashFlow)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Cashflows::CPI::InterpolationTypeEnum Cephei::QL::Cashflows::CCPICashFlow::Interpolation::get ()
{
    try
    {
    	QuantLib::CPI::InterpolationType _rv = (QuantLib::CPI::InterpolationType)(*_ppCPICashFlow)->interpolation ( );   
        QL::Cashflows::CPI::InterpolationTypeEnum _nrv = (QL::Cashflows::CPI::InterpolationTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ICPICashFlow^ Cephei::QL::Cashflows::CCPICashFlow_Factory::Create (Double notional, Cephei::QL::Indexes::IZeroInflationIndex^ index, DateTime baseDate, Double baseFixing, DateTime fixingDate, DateTime paymentDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ growthOnly, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>^ interpolation, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ frequency)
{
    return REFNEW CCPICashFlow ( notional,  index,  baseDate,  baseFixing,  fixingDate,  paymentDate,  growthOnly,  interpolation,  frequency);
}
